package com.rttmall.shopbackend.app.gambling.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.fastjson.JSON;
import com.rttmall.shopbackend.app.gambling.pojo.GamblingRecordCustom;
import com.rttmall.shopbackend.app.gambling.service.GamblingRecordService;
import com.rttmall.shopbackend.enums.GamblingRecordAddressZiti;
import com.rttmall.shopbackend.pojo.Pagination;

@Controller
@RequestMapping("gamblingRecord")
public class GamblingRecordController {
	
	@Autowired
	GamblingRecordService gamblingRecordService;
	
	@RequestMapping(method = RequestMethod.GET)
    public String orderDetailPage() {
        return "app/gambling/prizeList";
    }

	@RequestMapping(value = "gamblingPeriodCustomer")
	public String gamblingPeriodCustomer() {
		return "app/gambling/PeriodCustomerList";
	}
	@RequestMapping(value = "/objectPrizeDetail")
    public String objectPrizeDetail() {
        return "app/gambling/objectPrizeDetail";
    }
	
	@RequestMapping(value = "/virtualPrizeDetail")
    public String virtualPrizeDetail() {
        return "app/gambling/virtualPrizeDetail";
    }
	@RequestMapping(value = "/queryGamblingRecord")
	public @ResponseBody Map queryGamblingRecord(
			@RequestParam(defaultValue = "") String json, Pagination grid) {
		GamblingRecordCustom gamblingRecordCustom = JSON.parseObject(json,
				GamblingRecordCustom.class);
		if (null == gamblingRecordCustom) {
			gamblingRecordCustom = new GamblingRecordCustom();
		}
		grid.setStartIndex(grid.getPageIndex() * grid.getPageSize());
		gamblingRecordCustom.setPagination(grid);
		Map map = gamblingRecordService.queryGamblingPeriodCustomer(gamblingRecordCustom);
		return map;
	}
	/**
	 * 查询实物列表
	 * @param json
	 * @param grid
	 * @return
	 * @throws Exception
	 */
    @RequestMapping(value = "/queryObjectPrizeShow")
    public
    @ResponseBody
    Map queryObjectProductShow(@RequestParam(defaultValue = "")String json, Pagination grid) throws Exception {
    	GamblingRecordCustom gamblingRecordCustom = JSON.parseObject(json, GamblingRecordCustom.class);
        if (null == gamblingRecordCustom) {
        	gamblingRecordCustom = new GamblingRecordCustom();
        }
        
        String productNameOrId = gamblingRecordCustom.getProductNameOrId();
        if(!StringUtils.isEmpty(productNameOrId)){
            boolean flag = org.apache.commons.lang.StringUtils.isNumeric(productNameOrId.trim());
            if(flag){
            	gamblingRecordCustom.setProductId(Integer.parseInt(productNameOrId.trim()));
            }else{
            	gamblingRecordCustom.setNameZh(productNameOrId.trim());
            }
        }
        
        grid.setStartIndex(grid.getPageIndex() * grid.getPageSize());
        gamblingRecordCustom.setPagination(grid);
        Map map = gamblingRecordService.queryObjectPrizeShow(gamblingRecordCustom);
        return map;
    }
    
    /**
	 * 查询虚拟列表
	 * @param json
	 * @param grid
	 * @return
	 * @throws Exception
	 */
    @RequestMapping(value = "/queryVirtualPrizeShow")
    public
    @ResponseBody
    Map queryVirtualPrizeShow(@RequestParam(defaultValue = "")String json, Pagination grid) throws Exception {
    	GamblingRecordCustom gamblingRecordCustom = JSON.parseObject(json, GamblingRecordCustom.class);
        if (null == gamblingRecordCustom) {
        	gamblingRecordCustom = new GamblingRecordCustom();
        }
        grid.setStartIndex(grid.getPageIndex() * grid.getPageSize());
        gamblingRecordCustom.setPagination(grid);
        Map map = gamblingRecordService.queryVirtualPrizeShow(gamblingRecordCustom);
        return map;
    }
    
    /**
	 * 编辑查询
	 * @param id
	 * @return
	 */
    @RequestMapping(value = "/editObjectPrize")
    public
    @ResponseBody
    GamblingRecordCustom editObjectPrize(@RequestParam String id) {
        return gamblingRecordService.editObjectPrize(Integer.parseInt(id.trim()));
    }
    
    /**
	 * 编辑查询
	 * @param id
	 * @return
	 */
    @RequestMapping(value = "/editVirtualPrize")
    public
    @ResponseBody
    GamblingRecordCustom editVirtualPrize(@RequestParam String id) {
        return gamblingRecordService.editVirtualPrize(Integer.parseInt(id.trim()));
    }
    
    /**
	 * 编辑查询所有物流名称
	 * @param id
	 * @return
	 */
    @RequestMapping(value = "/queryAllExpress")
    public
    @ResponseBody
    Map queryAllExpress() {
    	return gamblingRecordService.queryAllExpress();
    }
    
    /**
     * 更新地址信息（包含发货）
     * @param json
     */
    @RequestMapping(value = "/updateRecordAddressSubmit")
    public
    @ResponseBody
    void updateRecordAddressSubmit(@RequestParam String json) throws Exception{
    	GamblingRecordCustom gamblingRecordCustom = JSON.parseObject(json, GamblingRecordCustom.class);
    	gamblingRecordService.updateRecordAddressSubmit(gamblingRecordCustom);
    }
    
	@RequestMapping("/getZitiStatus")
	public @ResponseBody List getZitiStatus() {
		List list = new ArrayList<>();
		for (GamblingRecordAddressZiti e : GamblingRecordAddressZiti.values()) {
			if(e.getCode() != null){
				list.add(e);
			}
		}
		return list;
	}
}
